<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html lang = "en">

<head>

<link rel="shortcut icon" href="http://algs4.cs.princeton.edu/favicon.ico">
<link rel="stylesheet"    href="http://algs4.cs.princeton.edu/java.css" type="text/css">

<title>BellmanFordSP.java</title>

<meta HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=iso-8859-1">
<meta NAME="AUTHOR" CONTENT="Robert Sedgewick and Kevin Wayne">
<meta NAME="DESCRIPTION" CONTENT="BellmanFordSP code in Java">
<meta NAME="TITLE" CONTENT="BellmanFordSP code in Java">
<meta NAME="KEYWORDS" CONTENT="BellmanFordSP,java,programming,computer science,algorithm,data structure,program,code">
<meta NAME="ROBOTS" CONTENT="INDEX,FOLLOW">

</head>


<body>
<center><h1>BellmanFordSP.java</h1></center><p><br>

Below is the syntax highlighted version of <a href = "BellmanFordSP.java">BellmanFordSP.java</a>.
<p><br>

<!-- Generator: GNU source-highlight 3.1.6
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt><span class="comment">/******************************************************************************</span>
<span class="comment"> *  Compilation:  javac BellmanFordSP.java</span>
<span class="comment"> *  Execution:    java BellmanFordSP filename.txt s</span>
<span class="comment"> *  Dependencies: EdgeWeightedDigraph.java DirectedEdge.java Queue.java</span>
<span class="comment"> *                EdgeWeightedDirectedCycle.java</span>
<span class="comment"> *  Data files:   </span><span class="url">http://algs4.cs.princeton.edu/44sp/tinyEWDn.txt</span>
<span class="comment"> *                </span><span class="url">http://algs4.cs.princeton.edu/44sp/mediumEWDnc.txt</span>
<span class="comment"> *</span>
<span class="comment"> *  Bellman-Ford shortest path algorithm. Computes the shortest path tree in</span>
<span class="comment"> *  edge-weighted digraph G from vertex s, or finds a negative cost cycle</span>
<span class="comment"> *  reachable from s.</span>
<span class="comment"> *</span>
<span class="comment"> *  % java BellmanFordSP tinyEWDn.txt 0</span>
<span class="comment"> *  0 to 0 ( 0.00)  </span>
<span class="comment"> *  0 to 1 ( 0.93)  0-&gt;2  0.26   2-&gt;7  0.34   7-&gt;3  0.39   3-&gt;6  0.52   6-&gt;4 -1.25   4-&gt;5  0.35   5-&gt;1  0.32</span>
<span class="comment"> *  0 to 2 ( 0.26)  0-&gt;2  0.26   </span>
<span class="comment"> *  0 to 3 ( 0.99)  0-&gt;2  0.26   2-&gt;7  0.34   7-&gt;3  0.39   </span>
<span class="comment"> *  0 to 4 ( 0.26)  0-&gt;2  0.26   2-&gt;7  0.34   7-&gt;3  0.39   3-&gt;6  0.52   6-&gt;4 -1.25   </span>
<span class="comment"> *  0 to 5 ( 0.61)  0-&gt;2  0.26   2-&gt;7  0.34   7-&gt;3  0.39   3-&gt;6  0.52   6-&gt;4 -1.25   4-&gt;5  0.35</span>
<span class="comment"> *  0 to 6 ( 1.51)  0-&gt;2  0.26   2-&gt;7  0.34   7-&gt;3  0.39   3-&gt;6  0.52   </span>
<span class="comment"> *  0 to 7 ( 0.60)  0-&gt;2  0.26   2-&gt;7  0.34   </span>
<span class="comment"> *</span>
<span class="comment"> *  % java BellmanFordSP tinyEWDnc.txt 0</span>
<span class="comment"> *  4-&gt;5  0.35</span>
<span class="comment"> *  5-&gt;4 -0.66</span>
<span class="comment"> *</span>
<span class="comment"> *</span>
<span class="comment"> ******************************************************************************/</span>

<span class="preproc">package</span><span class="normal"> edu</span><span class="symbol">.</span><span class="normal">princeton</span><span class="symbol">.</span><span class="normal">cs</span><span class="symbol">.</span><span class="normal">algs4</span><span class="symbol">;</span>

<span class="comment">/**</span>
<span class="comment"> *  The </span><span class="keyword">&lt;tt&gt;</span><span class="comment">BellmanFordSP</span><span class="keyword">&lt;/tt&gt;</span><span class="comment"> class represents a data type for solving the</span>
<span class="comment"> *  single-source shortest paths problem in edge-weighted digraphs with</span>
<span class="comment"> *  no negative cycles. </span>
<span class="comment"> *  The edge weights can be positive, negative, or zero.</span>
<span class="comment"> *  This class finds either a shortest path from the source vertex </span><span class="keyword">&lt;em&gt;</span><span class="comment">s</span><span class="keyword">&lt;/em&gt;</span>
<span class="comment"> *  to every other vertex or a negative cycle reachable from the source vertex.</span>
<span class="comment"> *  </span><span class="keyword">&lt;p&gt;</span>
<span class="comment"> *  This implementation uses the Bellman-Ford-Moore algorithm.</span>
<span class="comment"> *  The constructor takes time proportional to </span><span class="keyword">&lt;em&gt;</span><span class="comment">V</span><span class="keyword">&lt;/em&gt;</span><span class="comment"> (</span><span class="keyword">&lt;em&gt;</span><span class="comment">V</span><span class="keyword">&lt;/em&gt;</span><span class="comment"> + </span><span class="keyword">&lt;em&gt;</span><span class="comment">E</span><span class="keyword">&lt;/em&gt;</span><span class="comment">)</span>
<span class="comment"> *  in the worst case, where </span><span class="keyword">&lt;em&gt;</span><span class="comment">V</span><span class="keyword">&lt;/em&gt;</span><span class="comment"> is the number of vertices and </span><span class="keyword">&lt;em&gt;</span><span class="comment">E</span><span class="keyword">&lt;/em&gt;</span>
<span class="comment"> *  is the number of edges.</span>
<span class="comment"> *  Afterwards, the </span><span class="keyword">&lt;tt&gt;</span><span class="comment">distTo()</span><span class="keyword">&lt;/tt&gt;</span><span class="comment">, </span><span class="keyword">&lt;tt&gt;</span><span class="comment">hasPathTo()</span><span class="keyword">&lt;/tt&gt;</span><span class="comment">, and </span><span class="keyword">&lt;tt&gt;</span><span class="comment">hasNegativeCycle()</span><span class="keyword">&lt;/tt&gt;</span>
<span class="comment"> *  methods take constant time; the </span><span class="keyword">&lt;tt&gt;</span><span class="comment">pathTo()</span><span class="keyword">&lt;/tt&gt;</span><span class="comment"> and </span><span class="keyword">&lt;tt&gt;</span><span class="comment">negativeCycle()</span><span class="keyword">&lt;/tt&gt;</span>
<span class="comment"> *  method takes time proportional to the number of edges returned.</span>
<span class="comment"> *  </span><span class="keyword">&lt;p&gt;</span>
<span class="comment"> *  For additional documentation,    </span>
<span class="comment"> *  see </span><span class="keyword">&lt;a</span><span class="normal"> </span><span class="type">href</span><span class="symbol">=</span><span class="string">"http://algs4.cs.princeton.edu/44sp"</span><span class="keyword">&gt;</span><span class="comment">Section 4.4</span><span class="keyword">&lt;/a&gt;</span><span class="comment"> of    </span>
<span class="comment"> *  </span><span class="keyword">&lt;i&gt;</span><span class="comment">Algorithms, 4th Edition</span><span class="keyword">&lt;/i&gt;</span><span class="comment"> by Robert Sedgewick and Kevin Wayne. </span>
<span class="comment"> *</span>
<span class="comment"> *  </span><span class="type">@author</span><span class="comment"> Robert Sedgewick</span>
<span class="comment"> *  </span><span class="type">@author</span><span class="comment"> Kevin Wayne</span>
<span class="comment"> */</span>
<span class="keyword">public</span><span class="normal"> </span><span class="keyword">class</span><span class="normal"> </span><span class="classname">BellmanFordSP</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">    </span><span class="keyword">private</span><span class="normal"> </span><span class="type">double</span><span class="symbol">[]</span><span class="normal"> distTo</span><span class="symbol">;</span><span class="normal">               </span><span class="comment">// distTo[v] = distance  of shortest s-&gt;v path</span>
<span class="normal">    </span><span class="keyword">private</span><span class="normal"> DirectedEdge</span><span class="symbol">[]</span><span class="normal"> edgeTo</span><span class="symbol">;</span><span class="normal">         </span><span class="comment">// edgeTo[v] = last edge on shortest s-&gt;v path</span>
<span class="normal">    </span><span class="keyword">private</span><span class="normal"> </span><span class="type">boolean</span><span class="symbol">[]</span><span class="normal"> onQueue</span><span class="symbol">;</span><span class="normal">             </span><span class="comment">// onQueue[v] = is v currently on the queue?</span>
<span class="normal">    </span><span class="keyword">private</span><span class="normal"> </span><span class="usertype">Queue&lt;Integer&gt;</span><span class="normal"> queue</span><span class="symbol">;</span><span class="normal">          </span><span class="comment">// queue of vertices to relax</span>
<span class="normal">    </span><span class="keyword">private</span><span class="normal"> </span><span class="type">int</span><span class="normal"> cost</span><span class="symbol">;</span><span class="normal">                      </span><span class="comment">// number of calls to relax()</span>
<span class="normal">    </span><span class="keyword">private</span><span class="normal"> </span><span class="usertype">Iterable&lt;DirectedEdge&gt;</span><span class="normal"> cycle</span><span class="symbol">;</span><span class="normal">  </span><span class="comment">// negative cycle (or null if no such cycle)</span>

<span class="normal">    </span><span class="comment">/**</span>
<span class="comment">     * Computes a shortest paths tree from </span><span class="keyword">&lt;tt&gt;</span><span class="comment">s</span><span class="keyword">&lt;/tt&gt;</span><span class="comment"> to every other vertex in</span>
<span class="comment">     * the edge-weighted digraph </span><span class="keyword">&lt;tt&gt;</span><span class="comment">G</span><span class="keyword">&lt;/tt&gt;</span><span class="comment">.</span>
<span class="comment">     * </span><span class="type">@param</span><span class="comment"> G the acyclic digraph</span>
<span class="comment">     * </span><span class="type">@param</span><span class="comment"> s the source vertex</span>
<span class="comment">     * </span><span class="type">@throws</span><span class="comment"> IllegalArgumentException unless 0 </span><span class="preproc">&amp;le;</span><span class="comment"> </span><span class="keyword">&lt;tt&gt;</span><span class="comment">s</span><span class="keyword">&lt;/tt&gt;</span><span class="comment"> </span><span class="preproc">&amp;le;</span><span class="comment"> </span><span class="keyword">&lt;tt&gt;</span><span class="comment">V</span><span class="keyword">&lt;/tt&gt;</span><span class="comment"> - 1</span>
<span class="comment">     */</span>
<span class="normal">    </span><span class="keyword">public</span><span class="normal"> </span><span class="function">BellmanFordSP</span><span class="symbol">(</span><span class="usertype">EdgeWeightedDigraph</span><span class="normal"> G</span><span class="symbol">,</span><span class="normal"> </span><span class="type">int</span><span class="normal"> s</span><span class="symbol">)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">        distTo  </span><span class="symbol">=</span><span class="normal"> </span><span class="keyword">new</span><span class="normal"> </span><span class="type">double</span><span class="symbol">[</span><span class="normal">G</span><span class="symbol">.</span><span class="function">V</span><span class="symbol">()];</span>
<span class="normal">        edgeTo  </span><span class="symbol">=</span><span class="normal"> </span><span class="keyword">new</span><span class="normal"> DirectedEdge</span><span class="symbol">[</span><span class="normal">G</span><span class="symbol">.</span><span class="function">V</span><span class="symbol">()];</span>
<span class="normal">        onQueue </span><span class="symbol">=</span><span class="normal"> </span><span class="keyword">new</span><span class="normal"> </span><span class="type">boolean</span><span class="symbol">[</span><span class="normal">G</span><span class="symbol">.</span><span class="function">V</span><span class="symbol">()];</span>
<span class="normal">        </span><span class="keyword">for</span><span class="normal"> </span><span class="symbol">(</span><span class="type">int</span><span class="normal"> v </span><span class="symbol">=</span><span class="normal"> </span><span class="number">0</span><span class="symbol">;</span><span class="normal"> v </span><span class="symbol">&lt;</span><span class="normal"> G</span><span class="symbol">.</span><span class="function">V</span><span class="symbol">();</span><span class="normal"> v</span><span class="symbol">++)</span>
<span class="normal">            distTo</span><span class="symbol">[</span><span class="normal">v</span><span class="symbol">]</span><span class="normal"> </span><span class="symbol">=</span><span class="normal"> Double</span><span class="symbol">.</span><span class="normal">POSITIVE_INFINITY</span><span class="symbol">;</span>
<span class="normal">        distTo</span><span class="symbol">[</span><span class="normal">s</span><span class="symbol">]</span><span class="normal"> </span><span class="symbol">=</span><span class="normal"> </span><span class="number">0.0</span><span class="symbol">;</span>

<span class="normal">        </span><span class="comment">// Bellman-Ford algorithm</span>
<span class="normal">        queue </span><span class="symbol">=</span><span class="normal"> </span><span class="keyword">new</span><span class="normal"> Queue</span><span class="symbol">&lt;</span><span class="normal">Integer</span><span class="symbol">&gt;();</span>
<span class="normal">        queue</span><span class="symbol">.</span><span class="function">enqueue</span><span class="symbol">(</span><span class="normal">s</span><span class="symbol">);</span>
<span class="normal">        onQueue</span><span class="symbol">[</span><span class="normal">s</span><span class="symbol">]</span><span class="normal"> </span><span class="symbol">=</span><span class="normal"> </span><span class="keyword">true</span><span class="symbol">;</span>
<span class="normal">        </span><span class="keyword">while</span><span class="normal"> </span><span class="symbol">(!</span><span class="normal">queue</span><span class="symbol">.</span><span class="function">isEmpty</span><span class="symbol">()</span><span class="normal"> </span><span class="symbol">&amp;&amp;</span><span class="normal"> </span><span class="symbol">!</span><span class="function">hasNegativeCycle</span><span class="symbol">())</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">            </span><span class="type">int</span><span class="normal"> v </span><span class="symbol">=</span><span class="normal"> queue</span><span class="symbol">.</span><span class="function">dequeue</span><span class="symbol">();</span>
<span class="normal">            onQueue</span><span class="symbol">[</span><span class="normal">v</span><span class="symbol">]</span><span class="normal"> </span><span class="symbol">=</span><span class="normal"> </span><span class="keyword">false</span><span class="symbol">;</span>
<span class="normal">            </span><span class="function">relax</span><span class="symbol">(</span><span class="normal">G</span><span class="symbol">,</span><span class="normal"> v</span><span class="symbol">);</span>
<span class="normal">        </span><span class="cbracket">}</span>

<span class="normal">        </span><span class="keyword">assert</span><span class="normal"> </span><span class="function">check</span><span class="symbol">(</span><span class="normal">G</span><span class="symbol">,</span><span class="normal"> s</span><span class="symbol">);</span>
<span class="normal">    </span><span class="cbracket">}</span>

<span class="normal">    </span><span class="comment">// relax vertex v and put other endpoints on queue if changed</span>
<span class="normal">    </span><span class="keyword">private</span><span class="normal"> </span><span class="type">void</span><span class="normal"> </span><span class="function">relax</span><span class="symbol">(</span><span class="usertype">EdgeWeightedDigraph</span><span class="normal"> G</span><span class="symbol">,</span><span class="normal"> </span><span class="type">int</span><span class="normal"> v</span><span class="symbol">)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">        </span><span class="keyword">for</span><span class="normal"> </span><span class="symbol">(</span><span class="usertype">DirectedEdge</span><span class="normal"> e </span><span class="symbol">:</span><span class="normal"> G</span><span class="symbol">.</span><span class="function">adj</span><span class="symbol">(</span><span class="normal">v</span><span class="symbol">))</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">            </span><span class="type">int</span><span class="normal"> w </span><span class="symbol">=</span><span class="normal"> e</span><span class="symbol">.</span><span class="function">to</span><span class="symbol">();</span>
<span class="normal">            </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">distTo</span><span class="symbol">[</span><span class="normal">w</span><span class="symbol">]</span><span class="normal"> </span><span class="symbol">&gt;</span><span class="normal"> distTo</span><span class="symbol">[</span><span class="normal">v</span><span class="symbol">]</span><span class="normal"> </span><span class="symbol">+</span><span class="normal"> e</span><span class="symbol">.</span><span class="function">weight</span><span class="symbol">())</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">                distTo</span><span class="symbol">[</span><span class="normal">w</span><span class="symbol">]</span><span class="normal"> </span><span class="symbol">=</span><span class="normal"> distTo</span><span class="symbol">[</span><span class="normal">v</span><span class="symbol">]</span><span class="normal"> </span><span class="symbol">+</span><span class="normal"> e</span><span class="symbol">.</span><span class="function">weight</span><span class="symbol">();</span>
<span class="normal">                edgeTo</span><span class="symbol">[</span><span class="normal">w</span><span class="symbol">]</span><span class="normal"> </span><span class="symbol">=</span><span class="normal"> e</span><span class="symbol">;</span>
<span class="normal">                </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(!</span><span class="normal">onQueue</span><span class="symbol">[</span><span class="normal">w</span><span class="symbol">])</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">                    queue</span><span class="symbol">.</span><span class="function">enqueue</span><span class="symbol">(</span><span class="normal">w</span><span class="symbol">);</span>
<span class="normal">                    onQueue</span><span class="symbol">[</span><span class="normal">w</span><span class="symbol">]</span><span class="normal"> </span><span class="symbol">=</span><span class="normal"> </span><span class="keyword">true</span><span class="symbol">;</span>
<span class="normal">                </span><span class="cbracket">}</span>
<span class="normal">            </span><span class="cbracket">}</span>
<span class="normal">            </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">cost</span><span class="symbol">++</span><span class="normal"> </span><span class="symbol">%</span><span class="normal"> G</span><span class="symbol">.</span><span class="function">V</span><span class="symbol">()</span><span class="normal"> </span><span class="symbol">==</span><span class="normal"> </span><span class="number">0</span><span class="symbol">)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">                </span><span class="function">findNegativeCycle</span><span class="symbol">();</span>
<span class="normal">                </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(</span><span class="function">hasNegativeCycle</span><span class="symbol">())</span><span class="normal"> </span><span class="keyword">return</span><span class="symbol">;</span><span class="normal">  </span><span class="comment">// found a negative cycle</span>
<span class="normal">            </span><span class="cbracket">}</span>
<span class="normal">        </span><span class="cbracket">}</span>
<span class="normal">    </span><span class="cbracket">}</span>

<span class="normal">    </span><span class="comment">/**</span>
<span class="comment">     * Is there a negative cycle reachable from the source vertex </span><span class="keyword">&lt;tt&gt;</span><span class="comment">s</span><span class="keyword">&lt;/tt&gt;</span><span class="comment">?</span>
<span class="comment">     * </span><span class="type">@return</span><span class="comment"> </span><span class="keyword">&lt;tt&gt;</span><span class="comment">true</span><span class="keyword">&lt;/tt&gt;</span><span class="comment"> if there is a negative cycle reachable from the</span>
<span class="comment">     *    source vertex </span><span class="keyword">&lt;tt&gt;</span><span class="comment">s</span><span class="keyword">&lt;/tt&gt;</span><span class="comment">, and </span><span class="keyword">&lt;tt&gt;</span><span class="comment">false</span><span class="keyword">&lt;/tt&gt;</span><span class="comment"> otherwise</span>
<span class="comment">     */</span>
<span class="normal">    </span><span class="keyword">public</span><span class="normal"> </span><span class="type">boolean</span><span class="normal"> </span><span class="function">hasNegativeCycle</span><span class="symbol">()</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">        </span><span class="keyword">return</span><span class="normal"> cycle </span><span class="symbol">!=</span><span class="normal"> </span><span class="keyword">null</span><span class="symbol">;</span>
<span class="normal">    </span><span class="cbracket">}</span>

<span class="normal">    </span><span class="comment">/**</span>
<span class="comment">     * Returns a negative cycle reachable from the source vertex </span><span class="keyword">&lt;tt&gt;</span><span class="comment">s</span><span class="keyword">&lt;/tt&gt;</span><span class="comment">, or </span><span class="keyword">&lt;tt&gt;</span><span class="comment">null</span><span class="keyword">&lt;/tt&gt;</span>
<span class="comment">     * if there is no such cycle.</span>
<span class="comment">     * </span><span class="type">@return</span><span class="comment"> a negative cycle reachable from the soruce vertex </span><span class="keyword">&lt;tt&gt;</span><span class="comment">s</span><span class="keyword">&lt;/tt&gt;</span><span class="comment"> </span>
<span class="comment">     *    as an iterable of edges, and </span><span class="keyword">&lt;tt&gt;</span><span class="comment">null</span><span class="keyword">&lt;/tt&gt;</span><span class="comment"> if there is no such cycle</span>
<span class="comment">     */</span>
<span class="normal">    </span><span class="keyword">public</span><span class="normal"> </span><span class="usertype">Iterable&lt;DirectedEdge&gt;</span><span class="normal"> </span><span class="function">negativeCycle</span><span class="symbol">()</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">        </span><span class="keyword">return</span><span class="normal"> cycle</span><span class="symbol">;</span>
<span class="normal">    </span><span class="cbracket">}</span>

<span class="normal">    </span><span class="comment">// by finding a cycle in predecessor graph</span>
<span class="normal">    </span><span class="keyword">private</span><span class="normal"> </span><span class="type">void</span><span class="normal"> </span><span class="function">findNegativeCycle</span><span class="symbol">()</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">        </span><span class="type">int</span><span class="normal"> V </span><span class="symbol">=</span><span class="normal"> edgeTo</span><span class="symbol">.</span><span class="normal">length</span><span class="symbol">;</span>
<span class="normal">        </span><span class="usertype">EdgeWeightedDigraph</span><span class="normal"> spt </span><span class="symbol">=</span><span class="normal"> </span><span class="keyword">new</span><span class="normal"> </span><span class="function">EdgeWeightedDigraph</span><span class="symbol">(</span><span class="normal">V</span><span class="symbol">);</span>
<span class="normal">        </span><span class="keyword">for</span><span class="normal"> </span><span class="symbol">(</span><span class="type">int</span><span class="normal"> v </span><span class="symbol">=</span><span class="normal"> </span><span class="number">0</span><span class="symbol">;</span><span class="normal"> v </span><span class="symbol">&lt;</span><span class="normal"> V</span><span class="symbol">;</span><span class="normal"> v</span><span class="symbol">++)</span>
<span class="normal">            </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">edgeTo</span><span class="symbol">[</span><span class="normal">v</span><span class="symbol">]</span><span class="normal"> </span><span class="symbol">!=</span><span class="normal"> </span><span class="keyword">null</span><span class="symbol">)</span>
<span class="normal">                spt</span><span class="symbol">.</span><span class="function">addEdge</span><span class="symbol">(</span><span class="normal">edgeTo</span><span class="symbol">[</span><span class="normal">v</span><span class="symbol">]);</span>

<span class="normal">        </span><span class="usertype">EdgeWeightedDirectedCycle</span><span class="normal"> finder </span><span class="symbol">=</span><span class="normal"> </span><span class="keyword">new</span><span class="normal"> </span><span class="function">EdgeWeightedDirectedCycle</span><span class="symbol">(</span><span class="normal">spt</span><span class="symbol">);</span>
<span class="normal">        cycle </span><span class="symbol">=</span><span class="normal"> finder</span><span class="symbol">.</span><span class="function">cycle</span><span class="symbol">();</span>
<span class="normal">    </span><span class="cbracket">}</span>

<span class="normal">    </span><span class="comment">/**</span>
<span class="comment">     * Returns the length of a shortest path from the source vertex </span><span class="keyword">&lt;tt&gt;</span><span class="comment">s</span><span class="keyword">&lt;/tt&gt;</span><span class="comment"> to vertex </span><span class="keyword">&lt;tt&gt;</span><span class="comment">v</span><span class="keyword">&lt;/tt&gt;</span><span class="comment">.</span>
<span class="comment">     * </span><span class="type">@param</span><span class="comment"> v the destination vertex</span>
<span class="comment">     * </span><span class="type">@return</span><span class="comment"> the length of a shortest path from the source vertex </span><span class="keyword">&lt;tt&gt;</span><span class="comment">s</span><span class="keyword">&lt;/tt&gt;</span><span class="comment"> to vertex </span><span class="keyword">&lt;tt&gt;</span><span class="comment">v</span><span class="keyword">&lt;/tt&gt;</span><span class="comment">;</span>
<span class="comment">     *    </span><span class="keyword">&lt;tt&gt;</span><span class="comment">Double.POSITIVE_INFINITY</span><span class="keyword">&lt;/tt&gt;</span><span class="comment"> if no such path</span>
<span class="comment">     * </span><span class="type">@throws</span><span class="comment"> UnsupportedOperationException if there is a negative cost cycle reachable</span>
<span class="comment">     *    from the source vertex </span><span class="keyword">&lt;tt&gt;</span><span class="comment">s</span><span class="keyword">&lt;/tt&gt;</span>
<span class="comment">     */</span>
<span class="normal">    </span><span class="keyword">public</span><span class="normal"> </span><span class="type">double</span><span class="normal"> </span><span class="function">distTo</span><span class="symbol">(</span><span class="type">int</span><span class="normal"> v</span><span class="symbol">)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">        </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(</span><span class="function">hasNegativeCycle</span><span class="symbol">())</span>
<span class="normal">            </span><span class="keyword">throw</span><span class="normal"> </span><span class="keyword">new</span><span class="normal"> </span><span class="function">UnsupportedOperationException</span><span class="symbol">(</span><span class="string">"Negative cost cycle exists"</span><span class="symbol">);</span>
<span class="normal">        </span><span class="keyword">return</span><span class="normal"> distTo</span><span class="symbol">[</span><span class="normal">v</span><span class="symbol">];</span>
<span class="normal">    </span><span class="cbracket">}</span>

<span class="normal">    </span><span class="comment">/**</span>
<span class="comment">     * Is there a path from the source </span><span class="keyword">&lt;tt&gt;</span><span class="comment">s</span><span class="keyword">&lt;/tt&gt;</span><span class="comment"> to vertex </span><span class="keyword">&lt;tt&gt;</span><span class="comment">v</span><span class="keyword">&lt;/tt&gt;</span><span class="comment">?</span>
<span class="comment">     * </span><span class="type">@param</span><span class="comment"> v the destination vertex</span>
<span class="comment">     * </span><span class="type">@return</span><span class="comment"> </span><span class="keyword">&lt;tt&gt;</span><span class="comment">true</span><span class="keyword">&lt;/tt&gt;</span><span class="comment"> if there is a path from the source vertex</span>
<span class="comment">     *    </span><span class="keyword">&lt;tt&gt;</span><span class="comment">s</span><span class="keyword">&lt;/tt&gt;</span><span class="comment"> to vertex </span><span class="keyword">&lt;tt&gt;</span><span class="comment">v</span><span class="keyword">&lt;/tt&gt;</span><span class="comment">, and </span><span class="keyword">&lt;tt&gt;</span><span class="comment">false</span><span class="keyword">&lt;/tt&gt;</span><span class="comment"> otherwise</span>
<span class="comment">     */</span>
<span class="normal">    </span><span class="keyword">public</span><span class="normal"> </span><span class="type">boolean</span><span class="normal"> </span><span class="function">hasPathTo</span><span class="symbol">(</span><span class="type">int</span><span class="normal"> v</span><span class="symbol">)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">        </span><span class="keyword">return</span><span class="normal"> distTo</span><span class="symbol">[</span><span class="normal">v</span><span class="symbol">]</span><span class="normal"> </span><span class="symbol">&lt;</span><span class="normal"> Double</span><span class="symbol">.</span><span class="normal">POSITIVE_INFINITY</span><span class="symbol">;</span>
<span class="normal">    </span><span class="cbracket">}</span>

<span class="normal">    </span><span class="comment">/**</span>
<span class="comment">     * Returns a shortest path from the source </span><span class="keyword">&lt;tt&gt;</span><span class="comment">s</span><span class="keyword">&lt;/tt&gt;</span><span class="comment"> to vertex </span><span class="keyword">&lt;tt&gt;</span><span class="comment">v</span><span class="keyword">&lt;/tt&gt;</span><span class="comment">.</span>
<span class="comment">     * </span><span class="type">@param</span><span class="comment"> v the destination vertex</span>
<span class="comment">     * </span><span class="type">@return</span><span class="comment"> a shortest path from the source </span><span class="keyword">&lt;tt&gt;</span><span class="comment">s</span><span class="keyword">&lt;/tt&gt;</span><span class="comment"> to vertex </span><span class="keyword">&lt;tt&gt;</span><span class="comment">v</span><span class="keyword">&lt;/tt&gt;</span>
<span class="comment">     *    as an iterable of edges, and </span><span class="keyword">&lt;tt&gt;</span><span class="comment">null</span><span class="keyword">&lt;/tt&gt;</span><span class="comment"> if no such path</span>
<span class="comment">     * </span><span class="type">@throws</span><span class="comment"> UnsupportedOperationException if there is a negative cost cycle reachable</span>
<span class="comment">     *    from the source vertex </span><span class="keyword">&lt;tt&gt;</span><span class="comment">s</span><span class="keyword">&lt;/tt&gt;</span>
<span class="comment">     */</span>
<span class="normal">    </span><span class="keyword">public</span><span class="normal"> </span><span class="usertype">Iterable&lt;DirectedEdge&gt;</span><span class="normal"> </span><span class="function">pathTo</span><span class="symbol">(</span><span class="type">int</span><span class="normal"> v</span><span class="symbol">)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">        </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(</span><span class="function">hasNegativeCycle</span><span class="symbol">())</span>
<span class="normal">            </span><span class="keyword">throw</span><span class="normal"> </span><span class="keyword">new</span><span class="normal"> </span><span class="function">UnsupportedOperationException</span><span class="symbol">(</span><span class="string">"Negative cost cycle exists"</span><span class="symbol">);</span>
<span class="normal">        </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(!</span><span class="function">hasPathTo</span><span class="symbol">(</span><span class="normal">v</span><span class="symbol">))</span><span class="normal"> </span><span class="keyword">return</span><span class="normal"> </span><span class="keyword">null</span><span class="symbol">;</span>
<span class="normal">        </span><span class="usertype">Stack&lt;DirectedEdge&gt;</span><span class="normal"> path </span><span class="symbol">=</span><span class="normal"> </span><span class="keyword">new</span><span class="normal"> Stack</span><span class="symbol">&lt;</span><span class="normal">DirectedEdge</span><span class="symbol">&gt;();</span>
<span class="normal">        </span><span class="keyword">for</span><span class="normal"> </span><span class="symbol">(</span><span class="usertype">DirectedEdge</span><span class="normal"> e </span><span class="symbol">=</span><span class="normal"> edgeTo</span><span class="symbol">[</span><span class="normal">v</span><span class="symbol">];</span><span class="normal"> e </span><span class="symbol">!=</span><span class="normal"> </span><span class="keyword">null</span><span class="symbol">;</span><span class="normal"> e </span><span class="symbol">=</span><span class="normal"> edgeTo</span><span class="symbol">[</span><span class="normal">e</span><span class="symbol">.</span><span class="function">from</span><span class="symbol">()])</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">            path</span><span class="symbol">.</span><span class="function">push</span><span class="symbol">(</span><span class="normal">e</span><span class="symbol">);</span>
<span class="normal">        </span><span class="cbracket">}</span>
<span class="normal">        </span><span class="keyword">return</span><span class="normal"> path</span><span class="symbol">;</span>
<span class="normal">    </span><span class="cbracket">}</span>

<span class="normal">    </span><span class="comment">// check optimality conditions: either </span>
<span class="normal">    </span><span class="comment">// (i) there exists a negative cycle reacheable from s</span>
<span class="normal">    </span><span class="comment">//     or </span>
<span class="normal">    </span><span class="comment">// (ii)  for all edges e = v-&gt;w:            distTo[w] &lt;= distTo[v] + e.weight()</span>
<span class="normal">    </span><span class="comment">// (ii') for all edges e = v-&gt;w on the SPT: distTo[w] == distTo[v] + e.weight()</span>
<span class="normal">    </span><span class="keyword">private</span><span class="normal"> </span><span class="type">boolean</span><span class="normal"> </span><span class="function">check</span><span class="symbol">(</span><span class="usertype">EdgeWeightedDigraph</span><span class="normal"> G</span><span class="symbol">,</span><span class="normal"> </span><span class="type">int</span><span class="normal"> s</span><span class="symbol">)</span><span class="normal"> </span><span class="cbracket">{</span>

<span class="normal">        </span><span class="comment">// has a negative cycle</span>
<span class="normal">        </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(</span><span class="function">hasNegativeCycle</span><span class="symbol">())</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">            </span><span class="type">double</span><span class="normal"> weight </span><span class="symbol">=</span><span class="normal"> </span><span class="number">0.0</span><span class="symbol">;</span>
<span class="normal">            </span><span class="keyword">for</span><span class="normal"> </span><span class="symbol">(</span><span class="usertype">DirectedEdge</span><span class="normal"> e </span><span class="symbol">:</span><span class="normal"> </span><span class="function">negativeCycle</span><span class="symbol">())</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">                weight </span><span class="symbol">+=</span><span class="normal"> e</span><span class="symbol">.</span><span class="function">weight</span><span class="symbol">();</span>
<span class="normal">            </span><span class="cbracket">}</span>
<span class="normal">            </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">weight </span><span class="symbol">&gt;=</span><span class="normal"> </span><span class="number">0.0</span><span class="symbol">)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">                System</span><span class="symbol">.</span><span class="normal">err</span><span class="symbol">.</span><span class="function">println</span><span class="symbol">(</span><span class="string">"error: weight of negative cycle = "</span><span class="normal"> </span><span class="symbol">+</span><span class="normal"> weight</span><span class="symbol">);</span>
<span class="normal">                </span><span class="keyword">return</span><span class="normal"> </span><span class="keyword">false</span><span class="symbol">;</span>
<span class="normal">            </span><span class="cbracket">}</span>
<span class="normal">        </span><span class="cbracket">}</span>

<span class="normal">        </span><span class="comment">// no negative cycle reachable from source</span>
<span class="normal">        </span><span class="keyword">else</span><span class="normal"> </span><span class="cbracket">{</span>

<span class="normal">            </span><span class="comment">// check that distTo[v] and edgeTo[v] are consistent</span>
<span class="normal">            </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">distTo</span><span class="symbol">[</span><span class="normal">s</span><span class="symbol">]</span><span class="normal"> </span><span class="symbol">!=</span><span class="normal"> </span><span class="number">0.0</span><span class="normal"> </span><span class="symbol">||</span><span class="normal"> edgeTo</span><span class="symbol">[</span><span class="normal">s</span><span class="symbol">]</span><span class="normal"> </span><span class="symbol">!=</span><span class="normal"> </span><span class="keyword">null</span><span class="symbol">)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">                System</span><span class="symbol">.</span><span class="normal">err</span><span class="symbol">.</span><span class="function">println</span><span class="symbol">(</span><span class="string">"distanceTo[s] and edgeTo[s] inconsistent"</span><span class="symbol">);</span>
<span class="normal">                </span><span class="keyword">return</span><span class="normal"> </span><span class="keyword">false</span><span class="symbol">;</span>
<span class="normal">            </span><span class="cbracket">}</span>
<span class="normal">            </span><span class="keyword">for</span><span class="normal"> </span><span class="symbol">(</span><span class="type">int</span><span class="normal"> v </span><span class="symbol">=</span><span class="normal"> </span><span class="number">0</span><span class="symbol">;</span><span class="normal"> v </span><span class="symbol">&lt;</span><span class="normal"> G</span><span class="symbol">.</span><span class="function">V</span><span class="symbol">();</span><span class="normal"> v</span><span class="symbol">++)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">                </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">v </span><span class="symbol">==</span><span class="normal"> s</span><span class="symbol">)</span><span class="normal"> </span><span class="keyword">continue</span><span class="symbol">;</span>
<span class="normal">                </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">edgeTo</span><span class="symbol">[</span><span class="normal">v</span><span class="symbol">]</span><span class="normal"> </span><span class="symbol">==</span><span class="normal"> </span><span class="keyword">null</span><span class="normal"> </span><span class="symbol">&amp;&amp;</span><span class="normal"> distTo</span><span class="symbol">[</span><span class="normal">v</span><span class="symbol">]</span><span class="normal"> </span><span class="symbol">!=</span><span class="normal"> Double</span><span class="symbol">.</span><span class="normal">POSITIVE_INFINITY</span><span class="symbol">)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">                    System</span><span class="symbol">.</span><span class="normal">err</span><span class="symbol">.</span><span class="function">println</span><span class="symbol">(</span><span class="string">"distTo[] and edgeTo[] inconsistent"</span><span class="symbol">);</span>
<span class="normal">                    </span><span class="keyword">return</span><span class="normal"> </span><span class="keyword">false</span><span class="symbol">;</span>
<span class="normal">                </span><span class="cbracket">}</span>
<span class="normal">            </span><span class="cbracket">}</span>

<span class="normal">            </span><span class="comment">// check that all edges e = v-&gt;w satisfy distTo[w] &lt;= distTo[v] + e.weight()</span>
<span class="normal">            </span><span class="keyword">for</span><span class="normal"> </span><span class="symbol">(</span><span class="type">int</span><span class="normal"> v </span><span class="symbol">=</span><span class="normal"> </span><span class="number">0</span><span class="symbol">;</span><span class="normal"> v </span><span class="symbol">&lt;</span><span class="normal"> G</span><span class="symbol">.</span><span class="function">V</span><span class="symbol">();</span><span class="normal"> v</span><span class="symbol">++)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">                </span><span class="keyword">for</span><span class="normal"> </span><span class="symbol">(</span><span class="usertype">DirectedEdge</span><span class="normal"> e </span><span class="symbol">:</span><span class="normal"> G</span><span class="symbol">.</span><span class="function">adj</span><span class="symbol">(</span><span class="normal">v</span><span class="symbol">))</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">                    </span><span class="type">int</span><span class="normal"> w </span><span class="symbol">=</span><span class="normal"> e</span><span class="symbol">.</span><span class="function">to</span><span class="symbol">();</span>
<span class="normal">                    </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">distTo</span><span class="symbol">[</span><span class="normal">v</span><span class="symbol">]</span><span class="normal"> </span><span class="symbol">+</span><span class="normal"> e</span><span class="symbol">.</span><span class="function">weight</span><span class="symbol">()</span><span class="normal"> </span><span class="symbol">&lt;</span><span class="normal"> distTo</span><span class="symbol">[</span><span class="normal">w</span><span class="symbol">])</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">                        System</span><span class="symbol">.</span><span class="normal">err</span><span class="symbol">.</span><span class="function">println</span><span class="symbol">(</span><span class="string">"edge "</span><span class="normal"> </span><span class="symbol">+</span><span class="normal"> e </span><span class="symbol">+</span><span class="normal"> </span><span class="string">" not relaxed"</span><span class="symbol">);</span>
<span class="normal">                        </span><span class="keyword">return</span><span class="normal"> </span><span class="keyword">false</span><span class="symbol">;</span>
<span class="normal">                    </span><span class="cbracket">}</span>
<span class="normal">                </span><span class="cbracket">}</span>
<span class="normal">            </span><span class="cbracket">}</span>

<span class="normal">            </span><span class="comment">// check that all edges e = v-&gt;w on SPT satisfy distTo[w] == distTo[v] + e.weight()</span>
<span class="normal">            </span><span class="keyword">for</span><span class="normal"> </span><span class="symbol">(</span><span class="type">int</span><span class="normal"> w </span><span class="symbol">=</span><span class="normal"> </span><span class="number">0</span><span class="symbol">;</span><span class="normal"> w </span><span class="symbol">&lt;</span><span class="normal"> G</span><span class="symbol">.</span><span class="function">V</span><span class="symbol">();</span><span class="normal"> w</span><span class="symbol">++)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">                </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">edgeTo</span><span class="symbol">[</span><span class="normal">w</span><span class="symbol">]</span><span class="normal"> </span><span class="symbol">==</span><span class="normal"> </span><span class="keyword">null</span><span class="symbol">)</span><span class="normal"> </span><span class="keyword">continue</span><span class="symbol">;</span>
<span class="normal">                </span><span class="usertype">DirectedEdge</span><span class="normal"> e </span><span class="symbol">=</span><span class="normal"> edgeTo</span><span class="symbol">[</span><span class="normal">w</span><span class="symbol">];</span>
<span class="normal">                </span><span class="type">int</span><span class="normal"> v </span><span class="symbol">=</span><span class="normal"> e</span><span class="symbol">.</span><span class="function">from</span><span class="symbol">();</span>
<span class="normal">                </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">w </span><span class="symbol">!=</span><span class="normal"> e</span><span class="symbol">.</span><span class="function">to</span><span class="symbol">())</span><span class="normal"> </span><span class="keyword">return</span><span class="normal"> </span><span class="keyword">false</span><span class="symbol">;</span>
<span class="normal">                </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">distTo</span><span class="symbol">[</span><span class="normal">v</span><span class="symbol">]</span><span class="normal"> </span><span class="symbol">+</span><span class="normal"> e</span><span class="symbol">.</span><span class="function">weight</span><span class="symbol">()</span><span class="normal"> </span><span class="symbol">!=</span><span class="normal"> distTo</span><span class="symbol">[</span><span class="normal">w</span><span class="symbol">])</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">                    System</span><span class="symbol">.</span><span class="normal">err</span><span class="symbol">.</span><span class="function">println</span><span class="symbol">(</span><span class="string">"edge "</span><span class="normal"> </span><span class="symbol">+</span><span class="normal"> e </span><span class="symbol">+</span><span class="normal"> </span><span class="string">" on shortest path not tight"</span><span class="symbol">);</span>
<span class="normal">                    </span><span class="keyword">return</span><span class="normal"> </span><span class="keyword">false</span><span class="symbol">;</span>
<span class="normal">                </span><span class="cbracket">}</span>
<span class="normal">            </span><span class="cbracket">}</span>
<span class="normal">        </span><span class="cbracket">}</span>

<span class="normal">        StdOut</span><span class="symbol">.</span><span class="function">println</span><span class="symbol">(</span><span class="string">"Satisfies optimality conditions"</span><span class="symbol">);</span>
<span class="normal">        StdOut</span><span class="symbol">.</span><span class="function">println</span><span class="symbol">();</span>
<span class="normal">        </span><span class="keyword">return</span><span class="normal"> </span><span class="keyword">true</span><span class="symbol">;</span>
<span class="normal">    </span><span class="cbracket">}</span>

<span class="normal">    </span><span class="comment">/**</span>
<span class="comment">     * Unit tests the </span><span class="keyword">&lt;tt&gt;</span><span class="comment">BellmanFordSP</span><span class="keyword">&lt;/tt&gt;</span><span class="comment"> data type.</span>
<span class="comment">     */</span>
<span class="normal">    </span><span class="keyword">public</span><span class="normal"> </span><span class="keyword">static</span><span class="normal"> </span><span class="type">void</span><span class="normal"> </span><span class="function">main</span><span class="symbol">(</span><span class="normal">String</span><span class="symbol">[]</span><span class="normal"> args</span><span class="symbol">)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">        </span><span class="usertype">In</span><span class="normal"> in </span><span class="symbol">=</span><span class="normal"> </span><span class="keyword">new</span><span class="normal"> </span><span class="function">In</span><span class="symbol">(</span><span class="normal">args</span><span class="symbol">[</span><span class="number">0</span><span class="symbol">]);</span>
<span class="normal">        </span><span class="type">int</span><span class="normal"> s </span><span class="symbol">=</span><span class="normal"> Integer</span><span class="symbol">.</span><span class="function">parseInt</span><span class="symbol">(</span><span class="normal">args</span><span class="symbol">[</span><span class="number">1</span><span class="symbol">]);</span>
<span class="normal">        </span><span class="usertype">EdgeWeightedDigraph</span><span class="normal"> G </span><span class="symbol">=</span><span class="normal"> </span><span class="keyword">new</span><span class="normal"> </span><span class="function">EdgeWeightedDigraph</span><span class="symbol">(</span><span class="normal">in</span><span class="symbol">);</span>

<span class="normal">        </span><span class="usertype">BellmanFordSP</span><span class="normal"> sp </span><span class="symbol">=</span><span class="normal"> </span><span class="keyword">new</span><span class="normal"> </span><span class="function">BellmanFordSP</span><span class="symbol">(</span><span class="normal">G</span><span class="symbol">,</span><span class="normal"> s</span><span class="symbol">);</span>

<span class="normal">        </span><span class="comment">// print negative cycle</span>
<span class="normal">        </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">sp</span><span class="symbol">.</span><span class="function">hasNegativeCycle</span><span class="symbol">())</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">            </span><span class="keyword">for</span><span class="normal"> </span><span class="symbol">(</span><span class="usertype">DirectedEdge</span><span class="normal"> e </span><span class="symbol">:</span><span class="normal"> sp</span><span class="symbol">.</span><span class="function">negativeCycle</span><span class="symbol">())</span>
<span class="normal">                StdOut</span><span class="symbol">.</span><span class="function">println</span><span class="symbol">(</span><span class="normal">e</span><span class="symbol">);</span>
<span class="normal">        </span><span class="cbracket">}</span>

<span class="normal">        </span><span class="comment">// print shortest paths</span>
<span class="normal">        </span><span class="keyword">else</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">            </span><span class="keyword">for</span><span class="normal"> </span><span class="symbol">(</span><span class="type">int</span><span class="normal"> v </span><span class="symbol">=</span><span class="normal"> </span><span class="number">0</span><span class="symbol">;</span><span class="normal"> v </span><span class="symbol">&lt;</span><span class="normal"> G</span><span class="symbol">.</span><span class="function">V</span><span class="symbol">();</span><span class="normal"> v</span><span class="symbol">++)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">                </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">sp</span><span class="symbol">.</span><span class="function">hasPathTo</span><span class="symbol">(</span><span class="normal">v</span><span class="symbol">))</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">                    StdOut</span><span class="symbol">.</span><span class="function">printf</span><span class="symbol">(</span><span class="string">"%d to %d (%5.2f)  "</span><span class="symbol">,</span><span class="normal"> s</span><span class="symbol">,</span><span class="normal"> v</span><span class="symbol">,</span><span class="normal"> sp</span><span class="symbol">.</span><span class="function">distTo</span><span class="symbol">(</span><span class="normal">v</span><span class="symbol">));</span>
<span class="normal">                    </span><span class="keyword">for</span><span class="normal"> </span><span class="symbol">(</span><span class="usertype">DirectedEdge</span><span class="normal"> e </span><span class="symbol">:</span><span class="normal"> sp</span><span class="symbol">.</span><span class="function">pathTo</span><span class="symbol">(</span><span class="normal">v</span><span class="symbol">))</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">                        StdOut</span><span class="symbol">.</span><span class="function">print</span><span class="symbol">(</span><span class="normal">e </span><span class="symbol">+</span><span class="normal"> </span><span class="string">"   "</span><span class="symbol">);</span>
<span class="normal">                    </span><span class="cbracket">}</span>
<span class="normal">                    StdOut</span><span class="symbol">.</span><span class="function">println</span><span class="symbol">();</span>
<span class="normal">                </span><span class="cbracket">}</span>
<span class="normal">                </span><span class="keyword">else</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">                    StdOut</span><span class="symbol">.</span><span class="function">printf</span><span class="symbol">(</span><span class="string">"%d to %d           no path</span><span class="specialchar">\n</span><span class="string">"</span><span class="symbol">,</span><span class="normal"> s</span><span class="symbol">,</span><span class="normal"> v</span><span class="symbol">);</span>
<span class="normal">                </span><span class="cbracket">}</span>
<span class="normal">            </span><span class="cbracket">}</span>
<span class="normal">        </span><span class="cbracket">}</span>

<span class="normal">    </span><span class="cbracket">}</span>

<span class="cbracket">}</span>

<span class="comment">/******************************************************************************</span>
<span class="comment"> *  Copyright 2002-2015, Robert Sedgewick and Kevin Wayne.</span>
<span class="comment"> *</span>
<span class="comment"> *  This file is part of algs4.jar, which accompanies the textbook</span>
<span class="comment"> *</span>
<span class="comment"> *      Algorithms, 4th edition by Robert Sedgewick and Kevin Wayne,</span>
<span class="comment"> *      Addison-Wesley Professional, 2011, ISBN 0-321-57351-X.</span>
<span class="comment"> *      </span><span class="url">http://algs4.cs.princeton.edu</span>
<span class="comment"> *</span>
<span class="comment"> *</span>
<span class="comment"> *  algs4.jar is free software: you can redistribute it and/or modify</span>
<span class="comment"> *  it under the terms of the GNU General Public License as published by</span>
<span class="comment"> *  the Free Software Foundation, either version 3 of the License, or</span>
<span class="comment"> *  (at your option) any later version.</span>
<span class="comment"> *</span>
<span class="comment"> *  algs4.jar is distributed in the hope that it will be useful,</span>
<span class="comment"> *  but WITHOUT ANY WARRANTY; without even the implied warranty of</span>
<span class="comment"> *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the</span>
<span class="comment"> *  GNU General Public License for more details.</span>
<span class="comment"> *</span>
<span class="comment"> *  You should have received a copy of the GNU General Public License</span>
<span class="comment"> *  along with algs4.jar.  If not, see </span><span class="url">http://www.gnu.org/licenses.</span>
<span class="comment"> ******************************************************************************/</span>
</tt></pre>

<script type="text/javascript">
var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));
</script>
<script type="text/javascript">
try {
var pageTracker = _gat._getTracker("UA-10811519-2");
pageTracker._trackPageview();
} catch(err) {}</script>

</body>

<p><br><address><small>
Last updated: Mon Mar 21 10:51:08 EDT 2016.
</small></address>

</html>
